package fwx.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

//生成验证码
@WebServlet(name = "CaptchaServlet", value = "/CaptchaServlet")
public class CaptchaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BasicRandomColorAngle(request,response);
    }
    /*
    产生随机验证码，验证码背景随机，字体整体随机
     */
    public void BasicColor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int width=100 ;
        int height=30;
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d=bufferedImage.createGraphics();
        //g2d.setColor(Color.white);
        Random r=new Random();
        g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
        g2d.fillRect(0,0,width,height);
        String captchaText=GernerateRadomCaptcha(4);
        ServletContext context=this.getServletContext();
        context.setAttribute("captcha",captchaText);
        g2d.setFont(new Font("微软雅黑",Font.BOLD,24));
        //g2d.setColor(Color.black);
        g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
        g2d.drawString(captchaText,10,25);
        addNoise(g2d,width,height);
        response.setContentType("image/png");
        OutputStream os=response.getOutputStream();
        ImageIO.write(bufferedImage,"png",os);
        os.close();
    }
    /*
    产生随机验证码，验证码背景随机，每个字体颜色随机
    */
    public void BasicRandomColor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int width=100 ;
        int height=30;
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d=bufferedImage.createGraphics();
        //g2d.setColor(Color.white);
        Random r=new Random();
        g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
        g2d.fillRect(0,0,width,height);
        //String captchaText=GernerateRadomCaptcha(4);
//        ServletContext context=this.getServletContext();
//        context.setAttribute("captcha",captchaText);
        StringBuilder stringBuilder=new StringBuilder();
        g2d.setFont(new Font("微软雅黑",Font.BOLD,24));
        //g2d.setColor(Color.black);
        int left=10;
        for (int i = 0; i <4 ; i++) {
            String str=GernerateRadomCaptcha(1);
            stringBuilder.append(str);
            g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
            g2d.drawString(str,left,25);
            left+=20;
        }
        addNoise(g2d,width,height);
        response.setContentType("image/png");
        OutputStream os=response.getOutputStream();
        ImageIO.write(bufferedImage,"png",os);
        os.close();
    }
    /*
  产生随机验证码，验证码背景随机，每个字体颜色随机,角度随机
   */
    public void BasicRandomColorAngle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int width=100 ;
        int height=30;
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d=bufferedImage.createGraphics();
        //g2d.setColor(Color.white);
        Random r=new Random();
        g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
        g2d.fillRect(0,0,width,height);
        //String captchaText=GernerateRadomCaptcha(4);
//        ServletContext context=this.getServletContext();
//        context.setAttribute("captcha",captchaText);
        StringBuilder stringBuilder=new StringBuilder();
        g2d.setFont(new Font("微软雅黑",Font.BOLD,24));
        //g2d.setColor(Color.black);
        int left=10;
        int x=15;
        for (int i = 0; i <4 ; i++) {
            String str=GernerateRadomCaptcha(1);
            stringBuilder.append(str);
            g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
            double th=r.nextInt(50)/100;//设置倾斜角度
            if(r.nextInt(2)==1){
                th=-th;
            }
            g2d.rotate(th,x,25);
            g2d.drawString(str,left,25);
            g2d.rotate(-th,x,25);
            left+=20;
            x+=20;
        }
        addNoise(g2d,width,height);
        response.setContentType("image/png");
        OutputStream os=response.getOutputStream();
        ImageIO.write(bufferedImage,"png",os);
        os.close();
    }
    public void addNoise(Graphics2D g2d,int width,int height){
        Random r=new Random();
        //加干扰点
        for (int i = 0; i < 20; i++) {
            g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
            g2d.drawOval(r.nextInt(width),r.nextInt(height),2,2);
        }
        //加干扰线
        for (int i = 0; i < 4; i++) {
            g2d.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
            g2d.setStroke(new BasicStroke(1.5f));
            g2d.drawLine(r.nextInt(width), r.nextInt(height),r.nextInt(width), r.nextInt(height) );
        }
    }
    private String GernerateRadomCaptcha(int length) {
        String charaters="ABCDEFGHIJIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
        StringBuilder stringBuilder=new StringBuilder();
        Random random=new Random();
        for (int i = 0; i <length ; i++) {
            int index=random.nextInt(charaters.length());
            stringBuilder.append(charaters.charAt(index));
        }
        return  stringBuilder.toString();
    }
}
